package com.rowenci.iot_agriculture.service.impl;

import com.rowenci.iot_agriculture.bean.Worker;
import com.rowenci.iot_agriculture.mapper.WorkerDao;
import com.rowenci.iot_agriculture.service.api.WorkerService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.*;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *
 * </p>
 *
 * @author rowenci
 * @date 2019/4/21 19:02
 */
@Service
public class WorkerServiceImpl implements WorkerService {

    @Resource
    private WorkerDao workerDao;

    @Override
    public Optional<Worker> findById(Integer id) {
        return workerDao.findById(id);
    }

    @Override
    public List<Worker> findAll() {
        return workerDao.findAll();
    }

    @Override
    public boolean addWorker(Worker worker) {
        if (workerDao.findWorkerByName(worker.getName()) == null) {
            if (worker.getName() == null || worker.getName() == "" ||
                    worker.getRfidId() == null) {
                return false;
            } else {
                workerDao.save(worker);
                if (workerDao.findWorkerByName(worker.getName()) == null) {
                    return false;
                } else {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean deleteWorkerByName(String name) {
        if (workerDao.findWorkerByName(name) == null) {
            return false;
        } else {
            workerDao.deleteWorkerByName(name);
            if (workerDao.findWorkerByName(name) == null) {
                return true;
            } else {
                return false;
            }
        }
    }

    @Override
    public boolean updateWorkerById(Worker worker) {
        String workername = worker.getName();
        Integer rfid_id = worker.getRfidId();
        if (workerDao.findById(worker.getId()) == null){
            return false;
        }else {
            workerDao.updateNameById(worker.getId(), workername);
            workerDao.updateRfidById(worker.getId(), rfid_id);
            return true;
        }
    }

    @Override
    public Worker findByRfid_id(Integer rfid_id) {
        if (workerDao == null){
            workerDao = new WorkerDao() {
                @Override
                public Worker findWorkerByName(String name) {
                    return null;
                }

                @Override
                public void deleteWorkerByName(String name) {

                }

                @Override
                public void updateNameById(Integer id, String name) {

                }

                @Override
                public void updateRfidById(Integer id, Integer rfid) {

                }

                @Override
                public Worker findByRfidId(Integer rfidId) {
                    Worker worker = new Worker();
                    String driver = "com.mysql.cj.jdbc.Driver";
                    String url = "jdbc:mysql://cdb-qhgx4ekn.gz.tencentcdb.com:10000/iot_agriculture?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true";
                    String username = "root";
                    String password = "bb3960519";
                    Connection conn = null;

                    try {
                        Class.forName(driver); //classLoader,加载对应驱动
                        conn = (Connection) DriverManager.getConnection(url, username, password);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    ResultSet i = null;
                    String sql = "select * from worker where rfid_id = '" + rfidId + "'";
                    PreparedStatement pstmt;
                    try {
                        pstmt = (PreparedStatement) conn.prepareStatement(sql);
                        i = pstmt.executeQuery();
                        while (i.next()){
                            worker.setId(i.getInt("id"));
                            worker.setRfidId(i.getInt("rfid_id"));
                            worker.setName(i.getString("name"));
                        }
                        pstmt.close();
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    return worker;
                }

                @Override
                public List<Worker> findAll() {
                    return null;
                }

                @Override
                public List<Worker> findAll(Sort sort) {
                    return null;
                }

                @Override
                public List<Worker> findAllById(Iterable<Integer> iterable) {
                    return null;
                }

                @Override
                public <S extends Worker> List<S> saveAll(Iterable<S> iterable) {
                    return null;
                }

                @Override
                public void flush() {

                }

                @Override
                public <S extends Worker> S saveAndFlush(S s) {
                    return null;
                }

                @Override
                public void deleteInBatch(Iterable<Worker> iterable) {

                }

                @Override
                public void deleteAllInBatch() {

                }

                @Override
                public Worker getOne(Integer integer) {
                    return null;
                }

                @Override
                public <S extends Worker> List<S> findAll(Example<S> example) {
                    return null;
                }

                @Override
                public <S extends Worker> List<S> findAll(Example<S> example, Sort sort) {
                    return null;
                }

                @Override
                public Page<Worker> findAll(Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Worker> S save(S s) {
                    return null;
                }

                @Override
                public Optional<Worker> findById(Integer integer) {
                    return Optional.empty();
                }

                @Override
                public boolean existsById(Integer integer) {
                    return false;
                }

                @Override
                public long count() {
                    return 0;
                }

                @Override
                public void deleteById(Integer integer) {

                }

                @Override
                public void delete(Worker worker) {

                }

                @Override
                public void deleteAll(Iterable<? extends Worker> iterable) {

                }

                @Override
                public void deleteAll() {

                }

                @Override
                public <S extends Worker> Optional<S> findOne(Example<S> example) {
                    return Optional.empty();
                }

                @Override
                public <S extends Worker> Page<S> findAll(Example<S> example, Pageable pageable) {
                    return null;
                }

                @Override
                public <S extends Worker> long count(Example<S> example) {
                    return 0;
                }

                @Override
                public <S extends Worker> boolean exists(Example<S> example) {
                    return false;
                }
            };
            return workerDao.findByRfidId(rfid_id);
        }
        return workerDao.findByRfidId(rfid_id);
    }
}
